package cn.achao.csr.openapi.controller;

import cn.achao.csr.openapi.core.model.req.DeepSeekReq;
import cn.achao.csr.openapi.core.model.resp.DeepSeekResp;
import cn.achao.csr.openapi.service.ThirdService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: achao
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/openapi/test")
public class OpenapiTestController {

    private final ThirdService<DeepSeekReq, DeepSeekResp> deepSeekChatService;

    @GetMapping("/aiChat")
    public String test(String msg) {
        DeepSeekReq req = new DeepSeekReq();
        List<DeepSeekReq.MessagesDTO> messages = new ArrayList<>();
        DeepSeekReq.MessagesDTO messagesDTO = new DeepSeekReq.MessagesDTO();
        messagesDTO.setRole("user");
        messagesDTO.setContent(msg);
        messages.add(messagesDTO);

        DeepSeekReq.ResponseFormatDTO responseFormatDTO = new DeepSeekReq.ResponseFormatDTO();
        responseFormatDTO.setType("text");

        req.setModel("deepseek-ai/DeepSeek-R1");
        req.setMessages(messages);
        req.setStream(false);
        req.setMax_tokens(512);
        req.setStop(new ArrayList<>());
        req.setTemperature(0.7);
        req.setTop_p(0.7);
        req.setTop_k(50);
        req.setFrequency_penalty(0.5);
        req.setN(1);
        req.setResponse_format(responseFormatDTO);


        DeepSeekResp deepSeekResp = deepSeekChatService.sendHttp(req, DeepSeekReq.class, DeepSeekResp.class);
        log.info("deepSeekResp:{}", deepSeekResp);
        List<DeepSeekResp.ChoicesDTO> choices = deepSeekResp.getChoices();
        if (!choices.isEmpty()) {
           return choices.get(0).getMessage().getContent();
        }
        return "error";
    }
}
